package org.NooLab.glue.net.http.rest;

import java.io.File;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.LogManager;




import org.NooLab.glue.net.UserData;
   
import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.PrintLog;
/*
import org.apache.ftpserver.ConnectionConfig;
import org.apache.ftpserver.ConnectionConfigFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FileSystemFactory;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
 
*/ 
import org.apache.ftpserver.FtpServerFactory;


public class _RestletServer {

	private java.util.logging.Logger jlogger ;
	private java.util.logging.Level loglevel ;
 
	// the respective part for ftp is from apache
	RestletServerFactory serverFactory ;
	_RestletHttpServer server;
	// from apache: ListenerFactory listenFactory ;
	// from apache: PropertiesUserManagerFactory userManagerFactory ;
	// from apache: UserManager usermgr ;
	// from apache: ConnectionConfigFactory connectionConfigFactory ;
	// from apache: ConnectionConfig connectionConfig; 
	// from apache: FileSystemFactory filesystemFactory ;
	File userMgrFile;

	
	String userMgrPropertiesFilename ;
	
	
	UserData baseBoxUser ;
	
	protected GlueRestlet restletPlug ;	
	GlueRestletHandlerIntf restGlue;
	
	int listeningPort = 8080;
	String restletRootDir = "" ;
	
	private int serverPrepared=0; 
	private int printLevel = 2 ;
	
 	DFutils fileutil = new DFutils() ;
 	PrintLog out = new PrintLog(printLevel,true);
 	
 	
	
	public _RestletServer( int listeningport , GlueRestletHandlerIntf restglue, UserData basicuser, int loglevel){
		 
		restGlue = restglue ;
		listeningPort =  listeningport ;
		
		baseBoxUser = basicuser ;
		
		constructCompletion(loglevel) ;
		
		customizeLogging();
	}
	
	public void init(){
		
		serverFactory = new RestletServerFactory();
		
		// all the necessary configuration
		
		
		server = serverFactory.createServer();
		
	}
	
	private void constructCompletion(int loglevel){
		
		prepareLogging(loglevel);
		
		restletPlug = new GlueRestlet( restGlue, printLevel );
		
		// restletsMapped.put("glueInterception", restletPlug) ;
		// this simple mapping gets replaced by a collection of DomainMapping 
		
	}

	private void prepareLogging( int levelAlias){
		
		jlogger = java.util.logging.Logger.getLogger("");
		
		if (levelAlias==0){
			printLevel = 1;
			loglevel = java.util.logging.Level.OFF ;
		}
		if (levelAlias==1){
			printLevel = 2;
			loglevel = java.util.logging.Level.OFF ;
		}
		if (levelAlias==2){
			printLevel = 2;
			loglevel = java.util.logging.Level.INFO ;
		}
		if (levelAlias>=3){
			printLevel = 4;
			loglevel = java.util.logging.Level.ALL ;
		}
		
		jlogger.setLevel(loglevel) ;
		
		/*
		Level level = Level.OFF;
		
		LogManager logmgr = LogManager.getLogManager();
		
		ConsoleHandler  clog = new ConsoleHandler();
		clog.setLevel( level);
		
		org.slf4j.Logger _logger = LoggerFactory.getLogger("ROOT");
		java.util.logging.Logger jlogger = logmgr.getLogger( _logger.getName() ) ;
		*/
	}

	private void customizeLogging(){
	
		Handler[]  handlers;
		Handler  handler;
		Filter logf;
		Enumeration<String> names ;
	
		/*
		 	remove console logging
		 	Logger rootlogger = Logger.getLogger("");
			rootlogger.removeHandler(rootlogger.getHandlers()[0]);
			
			for (Handler handler : Logger.getLogger("").getHandlers()) {
				handler.setLevel(Level.CONFIG);
			}
	
		 */
		handlers = jlogger.getHandlers();
		
		for (int i=0;i<handlers.length;i++){
			handler = handlers[i] ;
			logf = handler.getFilter() ;
			
		}
		
		LogManager logmgr = LogManager.getLogManager();
		
		names = logmgr.getLoggerNames();
		
		
		
	}

	
	public void start() {
		 
		
	}

	public void stop() {
		 
		
	}
	
	
	
	public int getListeningPort() {
		return listeningPort;
	}

	public void setListeningPort(int listeningPort) {
		this.listeningPort = listeningPort;
	}

	public int getPrintLevel() {
		return printLevel;
	}

	public void setPrintLevel(int printLevel) {
		this.printLevel = printLevel;
	}



	
	
	
	
	
	
	

}
